Skip to content

more robust rc solution#69

Closed
jamesholcombe wants to merge 2 commits intomainfrom
fix/signup-race-condition
Closed

more robust rc solution#69
jamesholcombe wants to merge 2 commits intomainfrom
fix/signup-race-condition

Conversation

@jamesholcombe
Copy link
Copy Markdown
Member

No description provided.

@jamesholcombe jamesholcombe requested a review from Copilot August 7, 2025 09:59
@vercel
Copy link
Copy Markdown

vercel Bot commented Aug 7, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
groundup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 7, 2025 10:08am
groundup-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 7, 2025 10:08am

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a more robust solution for handling race conditions in user creation by improving the createUser function. The changes focus on preventing duplicate key violations that can occur when multiple concurrent requests attempt to create the same user.

Key changes:

  • Enhanced database-level conflict handling with proper upsert logic
  • Added comprehensive documentation for the race condition handling approach
  • Simplified database queries by removing unnecessary compound conditions

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
packages/app/src/lib/auth.ts Added explanatory comments about race condition handling in user creation
packages/app/src/db/crud/user.ts Completely refactored createUser function with robust upsert logic and comprehensive documentation

.returning()
.onConflictDoUpdate({
target: [user.email],
target: [user.authId],
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conflict target has been changed from [user.email] to [user.authId], but the code also handles cases where users exist by email but not by authId (lines 70-94). This creates a logical inconsistency where the upsert won't handle email conflicts, potentially causing unique constraint violations on the email field if it has a unique constraint.

Suggested change
target: [user.authId],
target: [user.authId, user.email],

Copilot uses AI. Check for mistakes.
.returning()
.execute();

return updatedUser;
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function updates an existing user's authId when found by email (lines 78-93), but this could lead to security issues. If two different authentication providers return the same email address, this code would reassign the user account to the new authId, potentially giving unauthorized access to another user's account.

Copilot uses AI. Check for mistakes.
termsOfUseAcceptedAt: userInsert.termsOfUseAcceptedAt,
updatedAt: new Date(),
},
})
Copy link

Copilot AI Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment mentions handling 'remaining race conditions' but the earlier logic already handles user existence by both authId and email. This upsert clause may be redundant given the explicit checks above, making the code unnecessarily complex.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants